<?php

function check_schedule_conflicts($user_id,$timeslot)
{
	$timeslot = filter_var($timeslot, FILTER_SANITIZE_NUMBER_INT);
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql="SELECT * FROM `oh_regrecords` WHERE `user_id` = '".$user_id."' AND `timeslot` = '".$timeslot."'";
	
	$con=oh_sql_connect();
	
	$result=oh_sql_query($con,$sql);
	
	if($result->num_rows > 0) // user has another game in this timeslot
		return TRUE;
	else
		return FALSE;
	
}

function sync_user($user_id)
{
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	$con=oh_sql_connect();
	
	$sql = "UPDATE `oh_users` SET `slots_gm` = (SELECT count(*) FROM `oh_regrecords` where `user_id` = '".$user_id."' AND `is_gm` = '1'),
		`slots_player` = (SELECT count(*) FROM `oh_regrecords` where `user_id` = '".$user_id."' AND `is_gm` = '0')
		WHERE `uid` = '".$user_id."'";
		
	oh_sql_query($con,$sql);
}

function sync_game($game_id)
{
	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$con=oh_sql_connect();

	$sql = "UPDATE `oh_games` SET `cur_players` = (SELECT count(*) from `oh_regrecords` where `game_id` = '".$game_id."' AND `is_gm` = '0'),
		`cur_gms` = (SELECT count(*) from `oh_regrecords` where `game_id` = '".$game_id."' AND `is_gm` = '1')
		where `game_id` = '".$game_id."'";
		
	oh_sql_query($con,$sql);
}

function leave_game($game_id,$user_id)
{
	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	$was_gm = 0;
		
	$con=oh_sql_connect();

	$sql="SELECT * FROM `oh_regrecords` WHERE `game_id` = '".$game_id."' AND `user_id` = '".$user_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	if($result->num_rows > 0 && $row=$result->fetch_assoc()) 
	{
		$was_gm=$row['is_gm'];
		
		mysqli_free_result($result);
		
		$sql = "DELETE FROM `oh_regrecords` WHERE `game_id` = '".$game_id."' AND `user_id` = '".$user_id."'";
		
		oh_sql_query($con,$sql);
		
		sync_game($game_id);
/* DEPRECATED		
		if($was_gm==1)
			$sql = "UPDATE `oh_games` SET `cur_gms`=`cur_gms`-1 WHERE `game_id` = '".$game_id."'";
		else
			$sql = "UPDATE `oh_games` SET `cur_players`=`cur_players`-1 WHERE `game_id` = '".$game_id."'";
			
		oh_sql_query($con,$sql);
*/
		sync_user($user_id);
		
/*		DEPRECATED
		if($was_gm==1)
			$sql = "UPDATE `oh_users` SET `slots_gm`=`slots_gm`-1 WHERE `uid` = '".$user_id."'";
		else
			$sql = "UPDATE `oh_users` SET `slots_player`=`slots_player`-1 WHERE `uid` = '".$user_id."'";
			
		oh_sql_query($con,$sql);
*/		
			
		return TRUE;
	}	
	return FALSE;
}
	

function signup_gm_for_game($game_id,$user_id)
{
	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	
	$con=oh_sql_connect();
	
	$sql="SELECT `timeslot` FROM `oh_games` WHERE `game_id` = '".$game_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	if($result->num_rows > 0 && $row=$result->fetch_assoc()) 
	{
		$timeslot=$row['timeslot'];		
	}
	else
	{
		echo "Fatal error: unable to locate game!";
		die();
	}
	
	mysqli_free_result($result);
	
	if(check_schedule_conflicts($user_id,$timeslot)==TRUE)
	{
		return "CONFLICT";
	}
	else	
	{
		$sql="INSERT INTO `oh_regrecords` (`game_id`,`user_id`,`timeslot`,`is_gm`) VALUES (
			'".$game_id."',
			'".$user_id."',
			'".$timeslot."',
			'1')";
			
		oh_sql_query($con,$sql);
		
		sync_game($game_id);
		
/* deprecated		
		$sql="UPDATE `oh_games` SET `cur_gms`=`cur_gms`+1 WHERE `game_id` = '".$game_id."'";
		
		oh_sql_query($con,$sql);

		$sql = "UPDATE `oh_users` SET `slots_gm`=`slots_gm`+1 WHERE `uid` = '".$user_id."'";
		
		oh_sql_query($con,$sql);		
*/
		sync_user($user_id);

		return "SIGNEDUP";
	}
	
}

function signup_player_for_game($game_id,$user_id,$role,$level)
{
	$con=oh_sql_connect();

	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	$level = filter_var($level, FILTER_SANITIZE_NUMBER_INT);
	$role=$con->real_escape_string($role);
	
	$sql="SELECT `timeslot` FROM `oh_games` WHERE `game_id` = '".$game_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	if($result->num_rows > 0 && $row=$result->fetch_assoc()) 
	{
		$timeslot=$row['timeslot'];		
	}
	else
	{
		echo "Fatal error: unable to locate game!";
		die();
	}
	
	mysqli_free_result($result);
	
	if(check_schedule_conflicts($user_id,$timeslot)==TRUE)
	{
		return "CONFLICT";
	}
	else	
	{
		$sql="INSERT INTO `oh_regrecords` (`game_id`,`user_id`,`timeslot`,`is_gm`,`role`,`level`) VALUES (
			'".$game_id."',
			'".$user_id."',
			'".$timeslot."',
			'0',
			'".$role."',
			'".$level."')";
			
		oh_sql_query($con,$sql);
		
		sync_game($game_id);
		
/* deprecated		
	
		$sql="UPDATE `oh_games` SET `cur_players`=`cur_players`+1 WHERE `game_id` = '".$game_id."'";
		
		oh_sql_query($con,$sql);
		
		$sql = "UPDATE `oh_users` SET `slots_player`=`slots_player`+1 WHERE `uid` = '".$user_id."'";
		
		oh_sql_query($con,$sql);	
*/
		sync_user($user_id);		
					
		return "SIGNEDUP";
	}
	
}

function oh_render_user_records_for_slot($slot_id,$user_id)
{
	$con=oh_sql_connect();

	$slot_id = filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql = "SELECT * FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` 
		INNER JOIN `oh_regrecords` ON `oh_regrecords`.`game_id` = `oh_games`.`game_id` WHERE `user_id` = '".$user_id."' and `oh_games`.`timeslot`='".$slot_id."'";
	
	echo "<td class=\"regtablebody\">\n";	
	
	$result=oh_sql_query($con,$sql);
	
	if($result->num_rows > 0 && $row=$result->fetch_assoc()) 
	{
		echo "<span class=\"smalltext\">".$row['game_location']."</span><br />\n";
		echo "[".$row[system]."] ".$row[title]."<br />\n";
		if($row['subtitle'] != "")
		{
			echo "<div align=\"right\">".$row[subtitle]."</div>\n";
		}
		if($row['is_gm']==1)
		{
			echo "<b>GMing</b><br />\n";
		} else {
			echo "<b>Playing</b> - ".$row['role']." ".$row['level']."<br />\n";
		}
	}
	else
	{
		echo "Open\n";
	}
	echo "</td>\n";
}
?>